package me.luger.core.commom.server;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.commom.util.JsonUtil;
import me.luger.core.commom.util.LogUtil;
import me.luger.core.commom.util.ServerUtil;

import java.util.HashMap;
import java.util.Map;

public class ServerInfo {

	public static final String PROFILE_DEV = "dev";
	public static final String PROFILE_TEST = "test";
	public static final String PROFILE_VSITE = "vsite";
	public static final String PROFILE_SITE = "site";

	public static Map<String, Object> info = new HashMap<String, Object>();

	private static final String KEY_HOST_IP = "HOST_IP";
	private static final String KEY_HOST_NAME = "HOST_NAME";
	private static final String KEY_SERVICE_NAME = "SERVICE_NAME";
	private static final String KEY_SERVICE_NUM = "SERVICE_NUM";
	private static final String KEY_SERVICE_START_TIME = "SERVICE_START_TIME";
	private static final String KEY_SERVICE_VER = "SERVICE_VER";
	private static final String KEY_SERVICE_PROFILE = "SERVICE_PROFILE";

	public void init() {
		info.put(KEY_SERVICE_START_TIME, System.currentTimeMillis());
		String hostIp = ServerUtil.getLocalIp(ServerUtil.IP_TYPE_V4);
		info.put(KEY_HOST_IP, hostIp);
		String hostName = ServerUtil.getHostName();
		info.put(KEY_HOST_NAME, hostName);
		LogUtil.debugLog(JsonUtil.toString(info));
	}

	public void stop() {

	}

	public static String getServiceName() {
		Object obj = info.get(KEY_SERVICE_NAME);
		if (obj == null) {
			return null;
		}
		return (String) obj;
	}

	public void setServiceName(String serviceName) {
		info.put(KEY_SERVICE_NAME, serviceName);
	}

	public static String getServiceNum() {
		Object obj = info.get(KEY_SERVICE_NUM);
		if (obj == null) {
			return null;
		}
		return (String) obj;
	}

	public void setServiceNum(String serviceNum) {
		info.put(KEY_SERVICE_NUM, serviceNum);
	}

	public static String getServiceVer() {
		Object obj = info.get(KEY_SERVICE_VER);
		if (obj == null) {
			return null;
		}
		return (String) obj;
	}

	public void setServiceVer(String serviceVer) {
		info.put(KEY_SERVICE_VER, serviceVer);
	}

	public String getHostIp() {
		Object obj = info.get(KEY_HOST_IP);
		if (obj == null) {
			return null;
		}
		return (String) obj;
	}

	public static String getHostName() {
		Object obj = info.get(KEY_HOST_NAME);
		if (obj == null) {
			return null;
		}
		return (String) obj;
	}

	public void setProfile(String profile) {
		info.put(KEY_SERVICE_PROFILE, profile);
	}

	public static String getProfile() {
		Object obj = info.get(KEY_SERVICE_PROFILE);
		if (obj == null) {
			return null;
		}
		return (String) obj;
	}

	public static boolean isTest() {
		String profile = getProfile();
		if (PROFILE_DEV.equals(profile)) {
			return true;
		} else if (PROFILE_TEST.equals(profile)) {
			return true;
		} else {
			return false;
		}
	}

	public static Long getServiceStartTime() {
		Object obj = info.get(KEY_SERVICE_START_TIME);
		if (obj == null) {
			return null;
		}
		return (Long) obj;
	}

	public static String getServiceStartTime(String dataFormat) {
		Object obj = info.get(KEY_SERVICE_START_TIME);
		if (obj == null) {
			return null;
		}
		Long timeMillis = (Long) obj;
		return DateUtil.format(timeMillis, dataFormat);
	}

	public static Map<String, Object> getInfo() {
		return info;
	}

}
